package org.dxy.trigger.listener.route;

import com.google.common.eventbus.Subscribe;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dxy.domain.event.route.RouteDeletedEvent;
import org.dxy.infrastructure.persistent.db.mybatispuls.service.RouteViewService;
import org.dxy.infrastructure.persistent.db.mybatispuls.service.RouteProcessViewService;
import org.dxy.trigger.listener.EventListener;
import org.springframework.stereotype.Component;

/**
 * 工艺路线删除事件监听器
 * @author: dxy
 * @createTime: 2025/8/17 11:49
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RouteDeletedEventListener implements EventListener<RouteDeletedEvent> {
    
    private final RouteViewService routeViewService;
    private final RouteProcessViewService routeProcessViewService;
    
    @Override
    @Subscribe
    public void onEvent(RouteDeletedEvent event) {
        try {
            log.info("处理工艺路线删除事件: {}", event);

            // 删除工艺路线的工序关系
            routeProcessViewService.removeByRouteId(event.routeId());
            
            // 删除工艺路线读模型
            routeViewService.removeById(event.routeId());
            
            log.info("工艺路线删除事件处理完成, routeId={}", event.routeId());
        } catch (Exception e) {
            log.error("处理工艺路线删除事件失败", e);
            throw new RuntimeException("处理工艺路线删除事件失败", e);
        }
    }
}